/*
 * @Copyright: Copyright (c) 2019
 * @Author: caizhijun
 * @Version: 1.0
 * @Date: 2018-06-27 17:22:34
 */

cc.Class({
    extends: cc.Component,
    properties: {
    jumpHeight:0,
    jumpDuration:0,
    maxMoveSpeed:0,
    accel:0,
    jumpAudio:{
        default:null,
        type:cc.AudioClip
    },
    background:{
        default:null,
        type:cc.Node
    }
    },
    onLoad:function(){
        this.enabled=false;
        this.accLeft=false;
        this.accRight=false;
        this.xSpeed=0;
        this.setInputControl();
    },
    setJumpAction:function(){
        var jumpUp=cc.moveBy(this.jumpDuration,cc.v2(0,this.jumpHeight)).easing(cc.easeCubicActionOut());
        var jumpDown=cc.moveBy(this.jumpDuration,cc.v2(0,-this.jumpHeight)).easing(cc.easeCircleActionIn());
        var callback=cc.callFunc(this.playJumpSound,this);
        //return cc.repeatForever(cc.sequence(jumpUp,jumpDown,callback));
        return cc.repeatForever(cc.sequence(jumpUp,jumpDown));

        // var jumpUp = cc.moveBy(this.jumpDuration,cc.v2(0,this.jumpHeight)).easing(cc.easeCubicActionOut());
        // var jumpDown = cc.moveBy(this.jumpDuration,cc.v2(0,-this.jumpHeight)).easing(cc.easeCubicActionIn());
        // var callback = cc.callFunc(this.playJumpSound,this);
        // return cc.repeatForever(cc.sequence(jumpUp,jumpDown,callback));
    },
    playJumpSound:function(){
        cc.audioEngine.playEffect(this.jumpAudio,false);
    },
    setInputControl:function(){
        var self=this;
        cc.eventManager.addListener({
            event:cc.EventListener.KEYBOARD,
            onKeyPressed:function(keycode,event){
                switch(keycode){
                    case cc.KEY.a:
                          self.accLeft=true;
                          self.accRight=false;
                          break;
                    case cc.KEY.d:
                         self.accLeft=false;
                         self.accRight=true;
                         break;       
                }                
            },
            onKeyReleased:function(keycode,event){
                switch(keycode){
                    case cc.KEY.a:
                         self.accLeft=false;
                         break;
                    case cc.KEY.d:
                         self.accRight=false;
                         break;
                }
            }
        },self.node)
    },
    update:function(dt){
        if(this.accLeft){
           // console.log(this.node.getComponent(cc.Sprite).spriteFrame);
           // this.node.getComponent(cc.Sprite).spriteFrame =cc.url.raw("resources/monkey_l.png");
            //this.node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(cc.url.raw("resources/monkey_l.png"));
            if(this.node.scaleX>0){
              this.node.scaleX*=-1;
            }else{
              this.node.scaleX*=1;  
            }
           // console.log(this.node.spriteFrame);
            this.xSpeed-=this.accel*dt;
        }else if(this.accRight){
           //this.node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(cc.url.raw("resources/monkey_r.png"));
           if(this.node.scaleX>0){
            this.node.scaleX*=1;
          }else{
            this.node.scaleX*=-1;  
          }
            this.xSpeed+=this.accel*dt;
        }
        if(Math.abs(this.xSpeed)>this.maxMoveSpeed){
            this.xSpeed=this.maxMoveSpeed*this.xSpeed/Math.abs(this.xSpeed);
        }
        if(this.node.x<=-cc.winSize.width/2){
            if(this.xSpeed<0){
                this.xSpeed=0
            }
        }
        if(this.node.x>=cc.winSize.width/2){
            if(this.xSpeed>0){
                this.xSpeed=0;
            }
        }
        this.node.x+=this.xSpeed*dt;
        this.background.x-=this.xSpeed*dt*0.5;
    },
    stopMove:function(){
        this.enabled=false;
        this.xSpeed=0;
        this.accLeft=false;
        this.accRight=false;
        this.node.stopAllActions();
    },
    startMoveAt:function(pos){
        this.enabled=true;
        this.xSpeed=0;
        this.node.setPosition(pos);
        this.node.runAction(this.setJumpAction());
    }
});
